#ifndef __modbus_H
#define __modbus_H

#include "hl10.h"
#include <stdint.h>

#define  MODBUS_REG_READ			    0
#define  MODBUS_REG_WRITE			  1

enum 
{	
	RESPONSE_NULL = 0, 
	RESPONSE_FUN_CODE_ERR = 1, 
	RESPONSE_REG_ADDR_ERR = 2, 
	RESPONSE_DATA_ERR = 3, 
	RESPONSE_SLAVE_ERR = 4, 
	RESPONSE_SLAVE_BUSY = 6,
} ;

enum
{
	FUN_R_DO = 0x01,
	FUN_R_DI = 0x02,
	FUN_R_AO = 0x03,
	FUN_R_AI = 0x04,
	FUN_SW_DO = 0x05,
	FUN_MW_DO = 0x0f,
	FUN_SW_AO = 0x06,
	FUN_MW_AO = 0x10,
  FUN_REPORT = 0x45,
};

#define	REG_COM_AI_BAS_ADDR  0 
struct S_REG_COM_AI
{
	uint16_t 	hardware_version;
	uint16_t 	software_version;
	uint16_t 	equipment_year;
	uint16_t 	equipment_month;
	uint16_t 	equipment_day;
	uint16_t 	equipment_hour;
	uint16_t 	equipment_minute;
	uint16_t 	equipment_second;
	
	uint16_t 	ch1;
	uint16_t 	ch2;
	uint16_t	ch3;
	uint16_t 	ch4;
	uint16_t 	ch5;
	uint16_t 	ch6;
	
	uint16_t 	bus_ch1;
	uint16_t 	bus_ch2;
	uint16_t 	bus_ch3;
	uint16_t 	bus_ch4;
	uint16_t 	bus_sts;
	uint16_t 	sample_time_h;
	uint16_t 	sample_time_l;
	
	uint16_t 	clsoe_input_state;
	uint16_t 	h_bridge_state;
	uint16_t 	internal_adc_ch1;
	uint16_t 	internal_adc_ch2;
	uint16_t 	internal_adc_ch3;
	uint16_t 	internal_adc_ch4;
	uint16_t 	internal_adc_ch5;
	uint16_t 	internal_adc_ch6;
	uint16_t 	null1;
	uint16_t 	null2;
	uint16_t 	null3;
	uint16_t 	null4;
	uint16_t 	null5;
	uint16_t 	null6;
	uint16_t 	null7;
	uint16_t 	null8;
	uint16_t  null9;
	
	uint16_t 	gps_send_counter;
	uint16_t 	gps_csq;
	uint16_t 	ccid;
};

struct S_MODBUS
{
	uint8_t slave_addr;
  uint8_t  fun_code;   
	uint16_t reg_addr;  
	uint16_t  reg_base_addr;  
};

struct S_REG_MODBUS
{
	struct S_REG_HL10_ROLA_AO  s_reg_hl10_lora_ao;
  struct S_REG_COM_AI  s_reg_ai;
  struct S_MODBUS modbus;
  
  uint16_t  s_reg_valve_ao_size;
  uint16_t  s_reg_hl10_lora_ao_size;
  uint16_t  s_reg_ai_size;
};

void modbus_init(void);
void modbus_rx(void);
void modbus_report(void);

void port1_sensor_value_add(void);
#endif
